// -----------------------------------------------------------------------
//  <copyright file="CaptchaService.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://www.dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024-04-27 00:04</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Security;

using Lazy.Captcha.Core;


namespace Shares.Infrastructure.Security;

public class CaptchaService(ICaptcha captcha) : ICaptchaService
{
    /// <summary>
    /// 生成验证码
    /// </summary>
    /// <param name="id">验证码唯一标识</param>
    /// <param name="seconds">缓存秒数</param>
    /// <returns></returns>
    public (string code, byte[] bytes) Generate(string id, int? seconds = null)
    {
        var data = captcha.Generate(id, seconds);
        return (data.Code, data.Bytes);
    }

    /// <summary>
    /// 验证验证码
    /// </summary>
    /// <param name="id">验证码唯一标识</param>
    /// <param name="code">输入的验证码</param>
    /// <param name="removeIfSuccess">验证成功是否移除</param>
    /// <param name="removeIfFail">验证失败是否移除</param>
    /// <returns></returns>
    public bool Validate(string id, string code, bool removeIfSuccess = true, bool removeIfFail = true)
    {
        return captcha.Validate(id, code, removeIfSuccess, removeIfFail);
    }
}
